﻿Imports Microsoft.WindowsAPICodePack.Shell
Imports Microsoft.WindowsAPICodePack.Taskbar
Imports Microsoft.WindowsAPICodePack.Dialogs

Public Class Form1
    Private windowsTaskbar As TaskbarManager = TaskbarManager.Instance
    Private WithEvents buttonFirst As ThumbnailToolbarButton
    Private AppId As String = "C4F-Taskbar"

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Don't run unless the taskbar extensions are supported (currently Windows 7+).
        If Not TaskbarManager.IsPlatformSupported Then
            MessageBox.Show("This sample requires Windows 7 to run")
            Application.Exit()
        End If

        windowsTaskbar.ApplicationId = AppId
    End Sub

    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        ' Creates a button to be shown in the preview (thumbnail) view (hover over the taskbar)
        buttonFirst = New ThumbnailToolbarButton(My.Resources.search, "Search")

        ' Adds the button to the toolbar for the window (identified by the window handle)
        windowsTaskbar.ThumbnailToolbars.AddButtons(Me.Handle, buttonFirst)

        ' Populates the drop-down list  with progressbar states directly from the enumeration
        comboBoxProgressState.DataSource = System.Enum.GetNames(GetType(TaskbarProgressBarState))
        comboBoxProgressState.SelectedIndex = 0
    End Sub

    Private Sub trackProgress_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trackProgress.Scroll
        ' Set the progress bar based on the trackbar and update the taskbar to match
        progressBarValue.Value = trackProgress.Value
        windowsTaskbar.SetProgressValue(trackProgress.Value, 100) ' value, max
    End Sub

    Private Sub comboBoxProgressState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBoxProgressState.SelectedIndexChanged
        ' One-line method of updating the taskbar's progress state 
        ' to the enumerated value selected in the ComboBox
        windowsTaskbar.SetProgressState(CType(System.Enum.Parse(GetType(TaskbarProgressBarState), comboBoxProgressState.Text), TaskbarProgressBarState))
    End Sub

    Private Sub buttonBrowseIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonBrowseIcon.Click
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            ' Extract the icon from the selected file and set it as the overlay to the taskbar icon
            ' Useful for indicators like error, offline, or update
            Dim selIcon As Icon = Icon.ExtractAssociatedIcon(openFileDialog1.FileName)
            windowsTaskbar.SetOverlayIcon(Me.Handle, selIcon, "Overlay")
            lblIconFilename.Text = openFileDialog1.FileName
        End If
    End Sub

    Private Sub checkBoxPreviewClip_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkBoxPreviewClip.CheckedChanged
        ' Each tabbed thumbnail can be given a clipping region relative to the entire form's bitmap
        ' You can set a clipping region to the location and size of a certain control, as here,
        ' or choose arbitrary coordinates.
        If checkBoxPreviewClip.Checked Then
            windowsTaskbar.TabbedThumbnail.SetThumbnailClip(Me.Handle, New Rectangle(groupBox2.Location, groupBox2.Size))
        Else
            windowsTaskbar.TabbedThumbnail.SetThumbnailClip(Me.Handle, New Rectangle(0, 0, Me.Width, Me.Height))
        End If
    End Sub

    Private Sub buttonClearOverlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonClearOverlay.Click
        ' Clear the overlay by simply setting it to Nothing
        windowsTaskbar.SetOverlayIcon(Me.Handle, Nothing, Nothing)
        lblIconFilename.Text = ""
    End Sub

    Private Sub buttonFirst_Click(ByVal sender As Object, ByVal e As ThumbnailButtonClickedEventArgs) Handles buttonFirst.Click
        ' This event fires when the custom button in the taskbar preview is clicked
        MessageBox.Show("Search icon clicked")
    End Sub

End Class
